/**
 * Created by Administrator on 2018/5/19.
 * 素材分享方法
 */
const app = getApp(),
  _ = require('../Common/underscore.js'),
  {
    $resHost
  } = app.globalData;
/**
 *
 * @param type 素材类型
 * @param dataset 素材数据
 * @param material 素材 || userId
 * @returns {{title: *, path: (*|string), successCallback: (function())}}
 */
module.exports = (type, dataset, material, isVcard) => {
  const webViewUrl = '/pages/WebView/webView?url=user&webHash=';
  let {
      itemId,
      id,
      title,
      thumb = '',
      materialId,
      params = {},
      articleType,
      wechatArticleUrl,
      lotteryWay
    } = dataset,
    {
      name
    } = dataset,
    {
      shareEmployeeId,
      shareUserId
    } = app.getTrack(),
    userId = isVcard == 1 ? dataset.userId || material || xhs.getStorageSync('employeeInfo').id : shareEmployeeId,
    sshareUserId = isVcard == 1 ? dataset.userId || material || xhs.getStorageSync('employeeInfo').id : shareUserId,
    webViewQuery = encodeURIComponent(`shareEmployeeId=${userId}`),
    path = '',
    materialTypeName = '',
    mId = itemId || id;
  switch (type) {
    case '12':
      path = `${webViewUrl}coupon/manage/info/${mId}&query=${webViewQuery}`;
      thumb = this.data.couponImg;
      title = name + '赠送给您一张优惠券,赶快来领取吧~';
      materialTypeName = '优惠券';
      break;
    case '22':
      path = `${webViewUrl}store/service/${mId}&query=${webViewQuery}`;
      materialTypeName = '服务商品';
      break;
    case '6':
      path = `/pages/User/Activity/Info/info?id=${mId}&shareEmployeeId=${userId}`;
      materialTypeName = '报名';
      break;
    case '19':
      if (articleType == '2') {
        path = `/pages/WebView/webView?url=https&https=${wechatArticleUrl}`;
      } else {
        path = `/pages/User/Article/info?id=${mId}&shareEmployeeId=${userId}`;
      }
      materialTypeName = '文章';
      break;
    case '8':
      path = `/independentGoods/Goods/goods?goodsId=${mId}&shareEmployeeId=${userId}&shareUserId=${sshareUserId}`;
      materialTypeName = '商品';
      break;
    case '121':
      path = `/independentNewGoods/Goods/index?goodsId=${mId}&shareEmployeeId=${userId}&shareUserId=${sshareUserId}`;
      materialTypeName = '商品';
      break;
    case '26':
      path = `/pages/Employee/Material/caseDetail?id=${mId}&shareEmployeeId=${userId}`;
      materialTypeName = '案例';
      break;
    case '4':
      path = `/independentBargain/Bargain/Detail/detail?bargainId=${mId}&shareEmployeeId=${userId}&shareUserId=${sshareUserId}`;
      materialTypeName = '砍价';
      break;
    case '43':
      path = `/pages/User/Choose/index?id=${mId}&shareEmployeeId=${userId}`;
      materialTypeName = '评选';
      break;
    case '3':
      xhs.showShareMenu({
        withShareTicket: true
      });
      path = `/independentCollage/Collage/Detail/detail?id=${mId}&userId=${userId}&shareEmployeeId=${userId}&shareUserId=${sshareUserId}`;
      materialTypeName = '拼团';
      break;
    case '20':
      path = `/independentKill/Kill/Detail/detail?id=${mId}&shareEmployeeId=${userId}&shareUserId=${sshareUserId}`;
      materialTypeName = '秒杀';
      break;
    case '107':
      path = `/pages/User/CollectFavour/Details/details?id=${mId}&shareEmployeeId=${userId}`;
      materialTypeName = '集赞';
      materialId = _.isEmpty(materialId) ? [{
        url: `${$resHost}/img/miniprogram/luckDraw/default-img.jpg`,
        thumb: `${$resHost}/img/miniprogram/luckDraw/default-img.jpg`
      }] : materialId;
      break;
    case '106':
      path = `/pages/User/luckDraw/Detail/detail?id=${mId}&shareEmployeeId=${userId}`;
      materialTypeName = '免费';
      // if (type == '106' && (!dataset.params.materialId && dataset.params.mainPic && !dataset.params.mainPic.length) || (!dataset.params.mainPic && dataset.params.materialId && !dataset.params.materialId.length)) {
      //   materialId = [{
      //     thumb: `${$resHost}/img/miniprogram/luckDraw/default-img.jpg`
      //   }]
      // }

      //免费抽奖默认图片处理
      materialId = _.isEmpty(materialId) ? [{
        url: `${$resHost}/img/miniprogram/luckDraw/default-img.jpg`,
        thumb: `${$resHost}/img/miniprogram/luckDraw/default-img.jpg`
      }] : materialId;
      break;
    case '108':
      path = `/independentCrowdFunding/CrowdFunding/Detail/detail?id=${mId}&shareEmployeeId=${userId}&shareUserId=${sshareUserId}`;
      materialTypeName = '众筹';
      break;
    case '110':
      path = `/independentWishAssist/WishAssist/Detail/detail?id=${mId}&shareEmployeeId=${userId}&shareUserId=${sshareUserId}`;
      materialTypeName = '心愿购';
      break;
    case '113':
      path = `/pages/User/Jifu/index?id=${mId}&shareEmployeeId=${userId}`;
      materialTypeName = '集福';
      break;
    case '114':
      path = `/pages/User/Discover/index?id=${mId}&shareEmployeeId=${userId}&shareUserId=${userId}`;
      materialTypeName = '推荐有礼';
      break;
    case '120':
      path = `/pages/Employee/LiveStudio/index?id=${mId}&shareEmployeeId=${userId}&shareUserId=${userId}`;
      materialTypeName = '直播';
      break;
    case '112':
      if (lotteryWay == '1') {
        path = `/pages/User/Turn/Detail/detail?id=${mId}&shareEmployeeId=${userId}`;
      } else {
        path = `/pages/User/Turn/Tiger/detail?id=${mId}&shareEmployeeId=${userId}`;
      }
      materialTypeName = '转盘';
      //抽奖默认图片处理
      materialId = _.isEmpty(materialId) ? [{
        url: `${$resHost}/img/miniprogram/turn/banner2.jpg`,
        thumb: `${$resHost}/img/miniprogram/turn/banner2.jpg`
      }] : materialId;
      break;
    case '130':
      path = `/independentPresell/Detail/index?id=${mId}&shareEmployeeId=${userId}&shareUserId=${userId}`;
      materialTypeName = '预售';
      break;
  }
  return {
    materialTypeName,
    title,
    imageUrl: thumb || materialId[0].thumb,
    path
  };
};